Page({
  data: {
    userInfo: {
      nickname: '张三',
      avatarUrl: '/images/default-avatar.png',
      email: 'zhangsan@example.com'
    },
    settings: {
      notification: true,
      darkMode: false,
      autoPlay: true,
      fontSize: 16
    },
    fontSizeOptions: [14, 16, 18, 20, 22]
  },

  // 切换通知开关
  toggleNotification(e) {
    const { settings } = this.data
    settings.notification = e.detail.value
    this.setData({
      settings
    })
    wx.showToast({
      title: settings.notification ? '通知已开启' : '通知已关闭',
      icon: 'success'
    })
  },

  // 切换暗黑模式
  toggleDarkMode(e) {
    const { settings } = this.data
    settings.darkMode = e.detail.value
    this.setData({
      settings
    })
    wx.showToast({
      title: settings.darkMode ? '暗黑模式已开启' : '暗黑模式已关闭',
      icon: 'success'
    })
    // 实际应用中这里应该切换主题样式
  },

  // 切换自动播放
  toggleAutoPlay(e) {
    const { settings } = this.data
    settings.autoPlay = e.detail.value
    this.setData({
      settings
    })
    wx.showToast({
      title: settings.autoPlay ? '自动播放已开启' : '自动播放已关闭',
      icon: 'success'
    })
  },

  // 选择字体大小
  selectFontSize(e) {
    const { settings } = this.data
    settings.fontSize = this.data.fontSizeOptions[e.detail.value]
    this.setData({
      settings
    })
    wx.showToast({
      title: '字体大小已设置为 ' + settings.fontSize + 'px',
      icon: 'success'
    })
  },

  // 清除缓存
  clearCache() {
    wx.showModal({
      title: '提示',
      content: '确定要清除缓存吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '清除中...',
          })
          // 模拟清除缓存
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '缓存已清除',
              icon: 'success'
            })
          }, 1000)
        }
      }
    })
  },

  // 关于我们
  aboutUs() {
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态
          wx.removeStorageSync('token')
          wx.removeStorageSync('userInfo')
          // 跳转到登录页面
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }
      }
    })
  },

  onLoad: function () {
    // 从本地存储获取用户信息
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        userInfo
      })
    }

    // 从本地存储获取设置
    const settings = wx.getStorageSync('settings')
    if (settings) {
      this.setData({
        settings
      })
    }
  }
})